<template>
  <div>
    <div>
      <el-table-form ref="form" :model="query">
        <el-form-item class="merge-item">
          <el-select v-model="type" style="width: 170px; margin-right: 2px;">
            <el-option label="投保单号" value="insureCode" />
            <el-option label="客户编号" value="csmCode" />
            <el-option label="客户名称" value="csmName" />
          </el-select>
          <el-input v-model="txt" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="客户类型:" prop="csmType">
          <el-select v-model="query.csmType" filterable clearable placeholder="请选择">
            <el-option
              v-for="item in csmTypeMap"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态:">
          <el-select v-model="query.insureStatus" filterable clearable placeholder="请选择">
            <el-option
              v-for="item in insureStatusMap"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属机构:">
          <el-tree-select
            v-model="query.ckOrgSNNo"
            clearable
            class="tree-select"
            :popover-width="360"
            :select-params="{
              clearable: true,
              placeholder: '请选择',
              loading: $loadingPlugin.getTreeData
            }"
            :tree-params=" {
              'default-expand-all': true,
              'expand-on-click-node': false,
              'highlight-current': true,
              props:{
                value:'orgSNNo',
                label: 'orgName',
                disabled: 'disabled',
              },
              data: treeData
            }"
          />
        </el-form-item>
        <el-form-item label="变更原因:" prop="csmType">
          <el-select v-model="query.csmType" filterable clearable placeholder="请选择">
            <el-option
              v-for="item in reasonMap"
              :key="item.value"
              :label="item.desc"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <template v-slot:btns>
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
        </template>
      </el-table-form>
    </div>
    <div style="margin-bottom: 10px;" class="btn-group">
      <div>
        <el-button type="primary" icon="el-icon-brush" @click="add">同步变更</el-button>
      </div>
    </div>
    <lb-table
      ref="lbTable"
      v-loading="$loadingPlugin.fetchData"
      v-el-height-adaptive-table="{bottomOffset:10}"
      border
      highlight-current-row
      element-loading-text="加载中"
      :column="tableData.column"
      :data="tableData.data"
      pagination
      :total="tableData.total"
      custom-column
      @fetch="fetchData"
      @selection-change="handleSelectionChange"
    />
  </div>
</template>
<script>
  import { synchronizeIssureOrder, queryInsureChangeList, queryMap } from '@/api'
  import { formatColumnMap } from '@/utils'
  import getTreeData from '@/utils/jsonTree'
  import { mapGetters } from 'vuex'
  import { mergeAsyncFunc } from '@/utils/mergeAsyncFunc'

  export default {
    name: 'tabsOne',
    props: {
      csmTypeMap: {
        type: Array,
        required: true
      },
      insureStatusMap: {
        type: Array,
        required: true
      }
    },
    data() {
      return {
        multipleSelection: [],
        treeData: [],
        reasonMap: [],
        type: 'insureCode',
        txt: '',
        query: {
          csmType: '',
          insureStatus: '',
          ckOrgSNNo: ''
        },
        tableData: {
          total: 0,
          column: [
            {
              type: 'selection'
            },
            {
              prop: 'insureCode',
              label: '投保单号',
              width: 110
            },
            {
              prop: 'csmCode',
              label: '客户编号',
              width: 110
            },
            {
              prop: 'csmName',
              label: '客户名称'
            },
            {
              prop: 'csmPhone',
              label: '联系电话',
              width: 120
            },
            {
              prop: 'csmAddress',
              label: '客户地址',
              width: 120
            },
            {
              prop: 'newCsmAddress',
              label: '新客户地址',
              width: 120
            },
            {
              prop: 'csmType',
              label: '客户类型',
              render: (h, { row }) => <span>{ row.csmType && formatColumnMap(this.csmTypeMap, 'value', row.csmType, 'desc') }</span>
            },
            {
              prop: 'orgName',
              label: '所属机构',
              width: 150
            },
            {
              prop: 'insureStatus',
              label: '状态',
              render: (h, { row }) => <span style={row.insureStatus === -1 ? 'color: #F56C6C;' : null}>{ row.insureStatus && formatColumnMap(this.insureStatusMap, 'value', row.insureStatus, 'desc') }</span>
            },
            {
              prop: 'insureAddressRemindType',
              label: '变更原因',
              render: (h, { row }) => <span>{ row.insureAddressRemindType && formatColumnMap(this.reasonMap, 'value', row.insureAddressRemindType, 'desc') }</span>
            }
          ],
          data: []
        }
      }
    },
    computed: {
      ...mapGetters([
        'orgList'
      ])
    },
    async created() {
      await Promise.all([this.getTreeData(), this.queryOptionsMap()])
    },
    methods: {
      // 获取所属机构数据
      async getTreeData() {
        const data = this.orgList.map(item => ({
          label: item.name,
          ...item
        }))
        this.treeData = getTreeData(data)
      },
      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          ...this._getQuery(this.query),
          currentPage,
          pageSize
        }
        query[this.type] = this.txt
        const res = await queryInsureChangeList(query)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },
      // 查询枚举
      async queryOptionsMap() {
        const res = await mergeAsyncFunc(queryMap, { enumName: 'InsureAddressRemindType' })
        this.reasonMap = res.data // 变更原因
      },
      add() {
        if (this.multipleSelection.length === 0) {
          this.$message.error('请勾选需要同步的保单')
          return
        }
        this.$confirm('是否同步保单信息?', '提示', {
          confirmButtonText: '同步',
          cancelButtonText: '不同步',
          type: 'warning'
        }).then(() => {
          const loading = this.$message({
            message: '正在同步',
            duration: 0,
            iconClass: 'el-icon-loading'
          })
          synchronizeIssureOrder({ json: this.multipleSelection }).then(() => {
            this.$refs.lbTable.fetch()
            this.$emit('synchronize')
            this.$notify({
              title: '提示',
              message: '操作成功',
              type: 'success',
              duration: 2000
            })
          }).finally(() => {
            loading.close()
          })
        })
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      }
    }
  }
</script>

